(ns ura.Log
  (:require
   [reagent.core :as r]
   [reagent.dom :as rd]
   [stylefy.core :as sty]
   [stylefy.reagent :as stylefy-reagent]
   [ajax.core :refer [GET POST DELETE]]
   [ura.common :as uc :refer [log]]
   [ura.config :refer [getUrl]]
   [ura.ui :as ui]
   [ura.style :as style :refer [attr]]
   [ura.table :refer [MyTable]]

   ["antd" :as ant]
   [re-com.core :as rc :refer [at]]
   ))

(def tableData (r/atom {:data []
                        :page {:current 1 :page-size 15 :total 0}
                        }))

(def param (r/atom {}))

(defn loadData []
  (log "query log data ")
  (GET (getUrl "/ura/log")
       {:params @param
        :response-format :json
        :keywords? true
        :handler
        (fn [resp]
          (log "get log resp " resp)
          (swap! tableData assoc :data resp)
          )
        })
  )

(def modelList (r/atom []))
(defn loadModelList []
  (GET (getUrl "/ura/log/modelList")
       {:response-format :json
        :keywords? true
        :handler (fn [resp]
                   (log "get log modelList " resp)
                   (reset! modelList resp)
                   )
        }
       )
  )

;;; ui

(defn searchForm []
  (fn []
    [:div (attr :flex :gap=10px)
     ;; [:> ant/Button {:type "primary"} "查询"]

     [ui/my-label "模块："]
     [rc/single-dropdown {:src (rc/at)
                          :on-change #(swap! param assoc :model %)
                          :model (r/cursor param [:model])
                          :choices modelList
                          :width "260px"
                          }]
     [ui/my-label "用户："]
     [ui/MyInput {:model (r/cursor param [:username])}]
     [ui/my-label "信息："]
     [ui/MyInput {:model (r/cursor param [:message])}]
     
     [:button.btn.btn-primary {:on-click #(loadData)}
      [:i.fa.fa-search (attr :mr=5)] "查询"]
     [:button.btn.btn-primary {:on-click #(do (reset! param {}) (loadData))}
      [:i.fa.fa-refresh (attr :mr=5)] "重置"]
     ]
    )
  )

(defn topBar []
  [ui/my-toolbar {}

   [:a.btn.btn-primary {:href  (getUrl "/ura/export/log")}
    [:span [:i.fa.fa-sign-out (attr :mr=5)] "导出"]
    ]
   [searchForm]
   ]
  )


(defn handleSelect [keys rows]
  (log "select row: " keys rows)
  (let [selected (js->clj rows :keywordize-keys true)]
    ;;(ant/message.info (str "You have selected: " (map :username selected)))
    )
  )

(defn datatable []
  [:> ant/Table
   {:dataSource @(r/cursor tableData [:data])
    :row-key "id"
    :size "small"
    :stripe "true"
    :columns [
              {:title "编号" :dataIndex "id"}
              {:title "日期" :dataIndex "log_date"}
              {:title "模块" :dataIndex "model_name"}
              {:title "级别" :dataIndex "level"}
              {:title "用户" :dataIndex "username"}
              {:title "信息" :dataIndex "message"}
              {:title "数据" :dataIndex "data"}
              ]

    :pagination {:show-size-changer true
                 :onShowSizeChange (fn [current pagesize]
                                     (swap! tableData assoc-in [:page :page-size] pagesize)
                                     (loadData)
                                     )
                 :onChange (fn [pagenumber]
                             (swap! tableData assoc-in [:page :current] pagenumber)
                             (loadData)
                             )
                 :page-size-options ["5" "10" "15" "30" "50" "100"]
                 :show-total #(str "总计 " % " 数据")
                 :current (:current (:page @tableData))
                 :page-size (:page-size (:page @tableData))
                 :total (:total (:page @tableData))
                 
                 }

    :rowClassName (fn [record index] (if (== 0 (mod index 2)) "tableStriped" ""))
    }])



(defn genPage []
  (loadData)
  (loadModelList)
  [:div 
   [topBar]
   [datatable]
   ]
)
